package com.telecom.at.front;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;
import com.telecom.at.db.dac.Dac;
import com.telecom.at.db.dac.Paging;
import com.telecom.at.db.model.AvgFlow;
import com.telecom.at.db.model.BWList;
import com.telecom.at.db.model.BasCrm3AData;
import com.telecom.at.db.model.NumOfAccount;
import com.telecom.at.db.model.SameMac;
import com.telecom.at.db.model.User;

@WebServlet("/front/ListSameMac")
public class ListSameMac extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
  
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String account = request.getParameter("account");
		if (account == null)
			account = "";

		String pageNoStr = request.getParameter("pageNo");
		if (pageNoStr == null)
			pageNoStr = "0";
		int pageNo = Integer.parseInt(pageNoStr);

		String pageSizeStr = request.getParameter("pageSize");
		if (pageSizeStr == null)
			pageSizeStr = "8";
		int pageSize = Integer.parseInt(pageSizeStr);

		if (request.getParameter("lastPage") != null)
			pageNo = Integer.MAX_VALUE;
		if (request.getParameter("firstPage") != null)
			pageNo = 0;
		if (request.getParameter("previousPage") != null)
			pageNo--;
		if (request.getParameter("nextPage") != null)
			pageNo++;
		
		
		
		List<SameMac> macList = null;
		//List<NumOfAccount> numList = null;
		User curUser = (User) request.getSession().getAttribute("curUser");
		if (curUser.getId() == 0) {
			
			macList = Dac.getInstance().allSameMacs();
			//numList = Dac.getInstance().numOfAccount();
		System.out.println("gg");}
		
		else   macList = Dac.getInstance().getSameMacsByCity(curUser.getCity());
			//flowList = Dac.getInstance().getAvgFlowOfUser(curUser.getCity());
		//List<AvgFlow> flowList = Dac.getInstance().findAvgFlowByAccount(account);
		//System.out.println(macList);
		/*List<BasCrm3AData> basCrm3AList = null;
		//List<NumOfAccount> numList = null;
        SameMac sameMac = (SameMac) request.getSession().getAttribute("sameMac");
        List<BWList>  bWList =Dac.getInstance().allBWList();
		Set<String> accountList=new HashSet<String>();
	       for(BWList bWList2:bWList)
	    	   accountList.add(bWList2.getAccount());
	       List<SameMac> flowList2 =new ArrayList<SameMac>();
			//basCrm3AList = Dac.getInstance().getAccountOfSameMac(basIp,leftMac);
			for(BasCrm3AData avgFlow:basCrm3AList) {
				if(!accountList.contains(avgFlow.getAccount())&&flowList2.size()<10) {
					
					flowList2.add(avgFlow);
				}
			}
			
			
			
		for(SameMac sameMac2:macList) {
			basCrm3AList = Dac.getInstance().getAccountOfSameMac(sameMac2.getBasIp(),sameMac2.getMac());
				
				flowList2.add(sameMac2);
			
		}
		*/
		Paging<SameMac> paging = new Paging<SameMac>(pageSize, pageNo, macList);
		request.setAttribute("paging", paging);
		request.setAttribute("account", account);
		request.setAttribute("macList", macList);
		//request.setAttribute("numList", numList);			
		request.getRequestDispatcher("/WEB-INF/jsp/front/ListSameMac.jsp").forward(request, response);		
	}

}
